package cn.ht.jywrwater.module.ht.service.customermeterdevice;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.customermeterdevice.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.customermeterdevice.CustomerMeterDeviceDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.customermeterdevice.CustomerMeterDeviceConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.customermeterdevice.CustomerMeterDeviceMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 小口径流量 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class CustomerMeterDeviceServiceImpl implements CustomerMeterDeviceService {

    @Resource
    private CustomerMeterDeviceMapper customerMeterDeviceMapper;

    @Override
    public Long createCustomerMeterDevice(CustomerMeterDeviceCreateReqVO createReqVO) {
        // 插入
        CustomerMeterDeviceDO customerMeterDevice = CustomerMeterDeviceConvert.INSTANCE.convert(createReqVO);
        customerMeterDeviceMapper.insert(customerMeterDevice);
        // 返回
        return customerMeterDevice.getId();
    }

    @Override
    public void updateCustomerMeterDevice(CustomerMeterDeviceUpdateReqVO updateReqVO) {
        // 校验存在
        validateCustomerMeterDeviceExists(updateReqVO.getId());
        // 更新
        CustomerMeterDeviceDO updateObj = CustomerMeterDeviceConvert.INSTANCE.convert(updateReqVO);
        customerMeterDeviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerMeterDevice(Long id) {
        // 校验存在
        validateCustomerMeterDeviceExists(id);
        // 删除
        customerMeterDeviceMapper.deleteById(id);
    }

    private void validateCustomerMeterDeviceExists(Long id) {
        if (customerMeterDeviceMapper.selectById(id) == null) {
            throw exception(CUSTOMER_METER_DEVICE_NOT_EXISTS);
        }
    }

    @Override
    public CustomerMeterDeviceDO getCustomerMeterDevice(Long id) {
        return customerMeterDeviceMapper.selectById(id);
    }

    @Override
    public List<CustomerMeterDeviceDO> getCustomerMeterDeviceList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return customerMeterDeviceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CustomerMeterDeviceDO> getCustomerMeterDevicePage(CustomerMeterDevicePageReqVO pageReqVO) {
        return customerMeterDeviceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CustomerMeterDeviceDO> getCustomerMeterDeviceList(CustomerMeterDeviceExportReqVO exportReqVO) {
        return customerMeterDeviceMapper.selectList(exportReqVO);
    }

}
